<?php

/**
 * For constructing anchor (link) elements, typically with an embedded image, and possible text.
 */
class LinkHelper extends AppHelper {
  
  var $helpers = array('Html');
  
  
  function __construct($options = null) {
  
    parent::__construct($options);
    
  } ///////////// end constructor /////////////////
  
  
  /**
   *
   * @param string $linkToController
   * @param string $linkToMethod
   * @param array  $linkArgs        Arguments to pass to the linkToMethod
   * @param bool   $separateArgs    Whether or not to pass all the arg separately on the url line
   *                                ('true') or to just pass the array itself intact ('false').
   * @param string $linkDisplayStr  A string that you want displayed as the link text.
   * @param string $tooltip         Text to be displayed on hover on the item.
   * @param string $imagePath        Relative to 'webroot/img'
   * @param array  $linkOptions
   * @param array  $imgOptions
   * @param string $confirmMsg
   * 
   * @return string        The <a> with optoinal child <img> and display text, etc. 
   */
  function makeLink($linkToController, $linkToMethod, $linkArgs = array(), $separateArgs = true,
                    $linkDisplayStr = null, $tooltip = null, $imagePath = null, 
                    $linkOptions = array(), $imgOptions = array(), $confirmMsg = null) {
    
    // Set the image 'alt' attribute
    if($linkDisplayStr && !isset($imgOptions['alt'])) {
      $imgOptions['alt'] = $linkDisplayStr;
    }
    else if($tooltip && !isset($imgOptions['alt'])) {
      $imgOptions['alt'] = $tooltip;
    }
    // Create the <img> element if caller passed in a path
    $imageHtml = '';
    if($imagePath) {
      $imageHtml = $this->Html->image($imagePath, $imgOptions);   
    }

    // Set the Controller/Action of the destination 
    $linkToArray = array('controller'=>$linkToController, 'action'=>$linkToMethod );
    // ... or set it link to current page
    if($linkToController==null || $linkToController== '') {
      $linkToArray = array('#' => ''); // just link to same page user is on (don't leave it)
    }
    // Add any arguments for the destination method to the URL array
    if($separateArgs) {
      foreach ($linkArgs as $arg ) {
        $linkToArray[] = $arg;
      }
    }
    else {
      $linkToArray[] = $linkArgs; // add entire array of args to the URL array
    }
    
    // Set necessary link options
    $linkOptions['escape'] = false; 
    if( !isset($linkOptions['title'])) $linkOptions['title'] = $tooltip; 
    // Set the display text string
    if(!$linkDisplayStr) {
      $linkDisplayStr = '';
    }
    // Make the <a> element
    $entireLinkContents = "{$imageHtml}$linkDisplayStr";
    $linkHtml = $this->Html->link($entireLinkContents, $linkToArray, $linkOptions, $confirmMsg);

    return $linkHtml;
    
  } ////////// end makeLink() //////////////////
  
  

} //////////////////////// end class ////////////////////////////////

?>
